<?php
namespace App\Library\System;

class Process
{

    private static $static_process;

    /**
     * Return Process Instant
     *
     * @return Process
     */
    public static function getInstance()
    {
        if (!self::$static_process) {
            self::$static_process = new Process();
        }

        return self::$static_process;
    }

    /**
     * Convert array to object
     *
     * @param $arr
     *
     * @return mixed
     */
    public static function arr2obj($arr)
    {
        return json_decode(json_encode($arr), false);
    }

    /**
     * Debug variable
     *
     * @param $variable
     */
    public static function debug($variable)
    {
        echo '<pre>';
        print_r($variable);
        echo '</pre>';
        die();
    }

    /**
     * Encode password
     *
     * @param $email
     * @param $hash_password
     *
     * @return string
     */
    public function encodePassword($email, $hash_password) // Encode second times
    {
        // $secure = '~!@#$%^&*()_+[]';
        // $hash_password = md5(sha1(password).$secure));

        $salt = 'D6Aa45zzTyOPA12309Pso981266';

        return md5(sha1($hash_password . $salt) . $email); // unique
    }

    public function hashID($id)
    {
        $hash_number = 1050382 + $id;
        $hash_table = 'APZPLMXYDW';
        $str_hash = strval($hash_number);
        $hash = '';
        for ($i = 0; $i < strlen($str_hash); $i++) {
            $k = intval($str_hash[$i]);
            $hash .= $hash_table[$k];
        }

        return $hash;
    }

    public static function cropImage($source_image, $target_image, $crop_area)
    {
        $source_file_name = basename($source_image);
        $source_image_type = substr($source_file_name, -3, 3);

        switch (strtolower($source_image_type)) {
            case 'jpg':
                $original_image = imagecreatefromjpeg($source_image);
                break;

            case 'gif':
                $original_image = imagecreatefromgif($source_image);
                break;

            case 'png':
                $original_image = imagecreatefrompng($source_image);
                break;

            default:
                trigger_error('cropImage(): Invalid source image type', E_USER_ERROR);

                return false;
        }

        $cropped_image = imagecreatetruecolor($crop_area['width'], $crop_area['height']);
        imagecopy($cropped_image, $original_image, 0, 0, $crop_area['left'], $crop_area['top'], $crop_area['width'], $crop_area['height']);
        $target_file_name = basename($target_image);
        $target_image_type = substr($target_file_name, -3, 3);

        switch (strtolower($target_image_type)) {
            case 'jpg':
                imagejpeg($cropped_image, $target_image, 100);
                break;

            case 'gif':
                imagegif($cropped_image, $target_image);
                break;

            case 'png':
                imagepng($cropped_image, $target_image, 0);
                break;

            default:
                trigger_error('cropImage(): Invalid target image type', E_USER_ERROR);
                imagedestroy($cropped_image);
                imagedestroy($original_image);

                return false;
        }
        imagedestroy($cropped_image);
        imagedestroy($original_image);

        return true;
    }

    public function validEmail($email)
    {

    }

    public function validPassword($password)
    {

    }

    public function splitWord($str)
    {
        return explode($str, ' ');
    }

    public function moreText($text, $n)
    {

    }

}